package com.paw.ruoyi.admin.controller.monitor;

import java.util.ArrayList;
import java.util.List;

import com.paw.ruoyi.admin.common.annotion.OperatorLog;
import com.paw.ruoyi.admin.common.annotion.RequiresPermissions;
import com.paw.ruoyi.admin.common.constant.BusinessType;
import com.paw.ruoyi.admin.common.constant.Constants;
import com.paw.ruoyi.admin.core.util.RedisUtilService;
import com.paw.ruoyi.admin.common.AjaxResult;
import com.paw.ruoyi.admin.common.TableDataInfo;
import com.paw.ruoyi.admin.controller.BaseController;
import com.paw.ruoyi.admin.domain.SysUserOnline;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 在线用户监控
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/api/monitor/online")
public class SysUserOnlineController extends BaseController {

//  @Autowired
//  private ISysUserOnlineService userOnlineService;

  @Autowired
  private RedisUtilService redisUtilService;

  @RequiresPermissions("monitor:online:list")
  @GetMapping("/list")
  public TableDataInfo list (String ipaddr, String userName) {
//    Collection<String> keys = redisCache.keys(Constants.LOGIN_TOKEN_KEY + "*");
    List<SysUserOnline> userOnlineList = new ArrayList<SysUserOnline>();
//    for (String key : keys) {
//      LoginUser loginUser = redisCache.getCacheObject(key);
//      if (StringUtils.isNotEmpty(ipaddr) && StringUtils.isNotEmpty(userName)) {
//        if (StringUtils.equals(ipaddr, loginUser.getIpaddr()) && StringUtils.equals(userName, loginUser.getUserName())) {
//          userOnlineList.add(userOnlineService.selectOnlineByInfo(ipaddr, userName, loginUser));
//        }
//      } else if (StringUtils.isNotEmpty(ipaddr)) {
//        if (StringUtils.equals(ipaddr, loginUser.getIpaddr())) {
//          userOnlineList.add(userOnlineService.selectOnlineByIpaddr(ipaddr, loginUser));
//        }
//      } else if (StringUtils.isNotEmpty(userName) && loginUser.getUserId()!=null) {
//        if (StringUtils.equals(userName, loginUser.getUserName())) {
//          userOnlineList.add(userOnlineService.selectOnlineByUserName(userName, loginUser));
//        }
//      } else {
//        userOnlineList.add(userOnlineService.loginUserToUserOnline(loginUser));
//      }
//    }
//    Collections.reverse(userOnlineList);
//    userOnlineList.removeAll(Collections.singleton(null));
    return getDataTable(userOnlineList);
  }

  /**
   * 强退用户
   */
  @RequiresPermissions("monitor:online:forceLogout")
  @OperatorLog(title = "在线用户", businessType = BusinessType.FORCE)
  @DeleteMapping("/{tokenId}")
  public AjaxResult forceLogout (@PathVariable String tokenId) {
    redisUtilService.del(Constants.LOGIN_TOKEN_KEY + tokenId);
    return AjaxResult.success();
  }
}
